//
//  SubjectNoEditViewController.m
//  SafeFoodManagerDemo
//
//  Created by bob on 2017/11/8.
//  Copyright © 2017年 bob. All rights reserved.
//

#import "SubjectNoEditViewController.h"
#import "SubjectUploadPicTableViewCell.h"
#import "SubjectNameTableViewCell.h"
#import "SubjectChildBtnTableViewCell.h"
#import "SubjectWriteTableViewCell.h"
#import "ChooseIsokTableViewCell.h"
#import "AlreadlyChangeTableViewCell.h"
#import "SubjectContentDetialModel.h"
#import "UILabel+NSAttributeString.h"
#import "HelpViewController.h"
@interface SubjectNoEditViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)TB_TableView *homeTableiew;

@property(nonatomic,strong)SubjectContentDetialModel *detialModel;


@end

@implementation SubjectNoEditViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.isScanphotos=YES;
    
     self.title=@"详情";

    [self loadDatas];
    
    [self addNavigationItemWithTitles:@[@"帮助"] isLeft:NO target:self action:@selector(helpClicked) tags:nil];
    
    [self addNavigationItemWithImageNames:@[@"返回白色"] Titles:@[@"返回"] isLeft:YES target:self action:@selector(backBtnClicked) tags:nil];
}

- (void)backBtnClicked
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)helpClicked
{
    HelpViewController *Patro=[HelpViewController new];
    JobListModel *list=[JobListModel new];
    list.pageOn = YES;
    list.pageSize = 10;
    list.currentPage = 1;
    
    Patro.ishidenSearch = YES;
    Patro.Reqmodel = list;
    Patro.isEdit=NO;
    Patro.isdeleMore=NO;
    Patro.isGroupStyle=YES;
    
    [self.navigationController pushViewController:Patro animated:YES];
}

-(TB_TableView*)homeTableiew
{
    if (!_homeTableiew) {
        
        _homeTableiew=[[TB_TableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStyleGrouped];
        
        _homeTableiew.delegate=self;
        
        _homeTableiew.dataSource=self;
        
        _homeTableiew.estimatedRowHeight=60;
        
        _homeTableiew.separatorStyle=UITableViewCellSeparatorStyleNone;
    
        _homeTableiew.tableFooterView=[[UIView alloc]init];
        
        [_homeTableiew registerNib:[UINib nibWithNibName:@"SubjectNameTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell1"];
        
        [_homeTableiew registerNib:[UINib nibWithNibName:@"SubjectChildBtnTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell2"];
        
        [_homeTableiew registerNib:[UINib nibWithNibName:@"SubjectWriteTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell3"];
        
        [_homeTableiew registerNib:[UINib nibWithNibName:@"ChooseIsokTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell4"];

        [_homeTableiew registerNib:[UINib nibWithNibName:@"AlreadlyChangeTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell6"];
        
        [_homeTableiew registerClass:[SubjectUploadPicTableViewCell class] forCellReuseIdentifier:@"PhotoCell"];
        
    }
    
    
    return _homeTableiew;
}





-(void)loadDatas
{
    [self showLoadingAnimation];
    
    BaseReqApi *Api=[[BaseReqApi alloc]initWithRequestUrl:@"/index.php/FoodCheckApi/inspectDetail.json" andrequestTime:5 andParams:@{@"logId":[NSString stringWithFormat:@"%ld",self.logs.logId]} andRequestMethod:YTKRequestMethodPOST andCache:NO andCacheTime:0 andPostToken:YES];
    
    [Api StarRequest:^(TBResponseStatus responseStatus, NSString *message, id responseObject) {
        
        [self stopLoadingAnimation];
        
        self.detialModel=[SubjectContentDetialModel mj_objectWithKeyValues:responseObject];
        
        if (responseStatus==1) {
            //请求成功
            
            [self.view addSubview:self.homeTableiew];
      
            
            [self.homeTableiew reloadData];
            
        }else if (responseStatus==0||responseStatus==2)
        {
            [MBProgressHUD showErrorMessage:message];
            
        }else{
            //404需要重新登录了
            
            [UIAlertView bk_showAlertViewWithTitle:message message:nil cancelButtonTitle:@"取消" otherButtonTitles:@[@"确定"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
                
                if (buttonIndex==1) {
                    
                    [self GotoLoginVC];
                }
                
            }];
            
        }
        
        
    }];
    
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 4;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    if (section==0) {
        
        return self.detialModel.data.child.count+1;
        
    }else if (section==1)
    {
        
        return 1;
        
    }else if (section==2)
    {
        
        return 3;
        
    }else{
        
        return 2;
        
    }
    
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        
        if (indexPath.row==0) {
            
            SubjectNameTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell1" forIndexPath:indexPath];
            
            if (self.detialModel.data.lev) {
                
                  cell.NameLabel.attributedText=[cell.NameLabel StartWithStr:[NSString stringWithFormat:@"%@☆",self.detialModel.data.name]];
                
            }else{
                
                cell.NameLabel.text=self.detialModel.data.name;
                
            }
            return cell;
            
        }else{
            
            ContentChild *child=self.detialModel.data.child[indexPath.row-1];
            
            if (child.isFill) {
                //可输入
                
                SubjectWriteTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell3" forIndexPath:indexPath];
                
                cell.isEnableEdit=NO;
                
                cell.child=child;
                
                return cell;
                
            }else{
                //不可输入
                
                SubjectChildBtnTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell2" forIndexPath:indexPath];
                
                cell.child=child;
                
                return cell;
                
            }
            
            
        }
        
        
    }else if (indexPath.section==1)
    {
        ChooseIsokTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell4" forIndexPath:indexPath];
        
        cell.isEnableEdit=NO;
        
        cell.isok=self.detialModel.data.isOk;
        
        return cell;
        
    }else if (indexPath.section==2)
    {
        
        if (indexPath.row==0||indexPath.row==2) {
            
            AlreadlyChangeTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell6" forIndexPath:indexPath];
            
            if (indexPath.row==0) {
                
                cell.TipLabel.text=@"补充说明";
                
                if (self.detialModel.data.remark.length==0) {
                    
                     cell.ContentLabel.text=@"无";
                    
                }else{
                
                cell.ContentLabel.text=self.detialModel.data.remark;
                }
                    
            }else{
                
                cell.TipLabel.text=@"整改建议";
                
                 if (self.detialModel.data.advise.length==0) {
                     
                     cell.ContentLabel.text=@"无";
                
                 }else{
                     
                     cell.ContentLabel.text=self.detialModel.data.advise;
                 }
                
            }
            
            return cell;
            
        }else{
            
            SubjectUploadPicTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"PhotoCell" forIndexPath:indexPath];
            
//            cell.TitleName=@"补充说明图片";
            
            cell.isEdit=NO;//是否可编辑
            cell.ImageArr=[HelpWay imageStrToArr:self.detialModel.data.remarkImg];//传递图片数组
            
            TBWeak;
            //浏览图片
            cell.ScanBlock=^(NSInteger num)
            {
                [weakSelf.ScanPhotoArr removeAllObjects];
                
                for (NSString*imagestr in [HelpWay imageStrToArr:self.detialModel.data.remarkImg]) {
                    
                    [weakSelf.ScanPhotoArr addObject:[MWPhoto photoWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",ReqUrl,imagestr]]]];
                    
                }
                
                [weakSelf GoToPhotoScanAndIndex:num];
                
            };
            
            return cell;
            
            
        }
        
    }else{
        
        if (indexPath.row==0) {
            
            AlreadlyChangeTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell6" forIndexPath:indexPath];
            
            cell.TipLabel.text=@"整改说明";
            
            if (self.detialModel.data.reformRemark.length==0) {
                
                cell.ContentLabel.text=@"无";
                
            }else{
            
            cell.ContentLabel.text=self.detialModel.data.reformRemark;
            
            }
            return cell;
            
        }else{
            
            SubjectUploadPicTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"PhotoCell" forIndexPath:indexPath];

            
            cell.isEdit=NO;//是否可编辑
            
            cell.ImageArr=[HelpWay imageStrToArr:self.detialModel.data.reformImage];//传递图片数组
            
            TBWeak;
            //浏览图片
            cell.ScanBlock=^(NSInteger num)
            {
                [weakSelf.ScanPhotoArr removeAllObjects];
                
                for (NSString*imagestr in [HelpWay imageStrToArr:self.detialModel.data.reformImage]) {
                    
                    [weakSelf.ScanPhotoArr addObject:[MWPhoto photoWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",ReqUrl,imagestr]]]];
                    
                }
                
                [weakSelf GoToPhotoScanAndIndex:num];
                
            };
            
            return cell;
            
        }
        
    }
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section==0) {
        return UITableViewAutomaticDimension;
        
    }else if (indexPath.section==1)
    {
        
        return 80;
        
    }else if (indexPath.section==2)
    {
        
        if (indexPath.row==0||indexPath.row==2) {
            
            return UITableViewAutomaticDimension;
            
        }else{
            
            NSArray *arr=[HelpWay imageStrToArr:self.detialModel.data.remarkImg];
            
            if (arr.count==0) {
                
                return 0;
                
            }else{
                
                NSInteger colorm=(arr.count-1)/3+1;//每行放三个，一个有多少行
                
                CGFloat cellHeight=(SCREEN_WIDTH-30-10*2)/3.0;
                
                return 15+10*(colorm-1)+colorm*cellHeight+21;
                
            }
            
        }
        
        
    }else{
        
        if (indexPath.row==0) {
            
            return UITableViewAutomaticDimension;
            
        }else{
            
            NSArray *arr=[HelpWay imageStrToArr:self.detialModel.data.reformImage];
            
            if (arr.count==0) {
                
                return 0;
                
            }else{
                
                NSInteger colorm=(arr.count-1)/3+1;//每行放三个，一个有多少行
                
                CGFloat cellHeight=(SCREEN_WIDTH-30-10*2)/3.0;
                
                return 15+10*(colorm-1)+colorm*cellHeight+21;
                
            }
            
        }
        
    }
    
}




-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    return 15;
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    
    return 0.1;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
